/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pingouin;

import java.awt.Color;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hippolyte
 */
public class Joueur implements Serializable {

    public int num;
    public String nom;
    public Color color;
    public boolean ordi;
    public int niveauOrdi;
    public ArrayList<Pingouin> pingouins = new ArrayList<>();
    public boolean serveur;
    public boolean surCetOrdi;
    public InetAddress address;

    public Joueur(int num, String nom, Color color, boolean IA, int niveauOrdi,
            boolean serveur, boolean surCetOrdi) {
        this.num = num;
        this.nom = nom;
        this.color = color;
        this.ordi = IA;
        this.niveauOrdi = niveauOrdi;
        this.serveur = serveur;
        this.surCetOrdi = surCetOrdi;
    }

    public int deplacementPossibles() {
        int nbDepl = 0;
        for (Pingouin p : pingouins) {
            ArrayList<Hexa> deplPossibles = p.hexa.deplacementsPossibles();
            if (!p.fini && deplPossibles != null) {
                nbDepl += deplPossibles.size();
            }

        }
        return nbDepl;
    }

    public ArrayList<Pingouin> PingouinDeplacementPossibles() {
        ArrayList<Pingouin> dep = new ArrayList<>();
        for (Pingouin p : pingouins) {
            ArrayList<Hexa> deplPossibles = p.hexa.deplacementsPossibles();
            if (deplPossibles != null && !deplPossibles.isEmpty() && !p.fini) {
                dep.add(p);
            }

        }
        return dep;
    }

    public int nbPoisson() {
        int nbPoiss = 0;
        for (Pingouin p : pingouins) {
            nbPoiss += p.nbPoissons;
        }
        return (nbPoiss);
    }

    public void joueOrdi() {

        if (niveauOrdi == 1) {
            joueOrdiNiv1();
        } else if (niveauOrdi == 2) {
            joueOrdiNiv2();
        } else if (niveauOrdi == 3) {
            joueOrdiNiv3();
        } else if (niveauOrdi == 4) {
            joueOrdiNiv4();
        } else if (niveauOrdi == 5) {
            joueOrdiNiv5();
        } else if (niveauOrdi == 6) {
            joueOrdiNiv6();
        }

    }

    public void joueOrdiNiv1() {
        ArrayList<Pingouin> dep = PingouinDeplacementPossibles();
        int choixPingouin = (int) (Math.random() * dep.size());
        Pingouin p = dep.get(choixPingouin);
        ArrayList<Hexa> hexa = p.hexa.deplacementsPossibles();
        int choixHexa = (int) (Math.random() * hexa.size());

        Partie.ressources.PingouinSelectionne = p;

        p.deplacer(hexa.get(choixHexa));
    }

    public void joueOrdiNiv2() {

        int maxPoisson = 0;
        Hexa maxHexa = null;
        Pingouin maxPingouin = null;
        for (Pingouin p : this.pingouins) {
            for (Hexa temp : p.hexa.deplacementsPossibles()) {
                if (temp.nbPoisson > maxPoisson) {
                    maxPoisson = temp.nbPoisson;
                    maxHexa = temp;
                    maxPingouin = p;
                }
            }
        }

        Partie.ressources.PingouinSelectionne = maxPingouin;

        maxPingouin.deplacer(maxHexa);
    }

    public void joueOrdiNiv3() {

        int maxDepl = -1;
        Hexa maxHexa = null;
        Pingouin maxPingouin = null;
        for (Pingouin p : this.pingouins) {
            for (Hexa temp : p.hexa.deplacementsPossibles()) {
                if (temp.deplacementsPossibles().size() > maxDepl) {
                    maxDepl = temp.deplacementsPossibles().size();
                    maxHexa = temp;
                    maxPingouin = p;
                }
            }
        }

        Partie.ressources.PingouinSelectionne = maxPingouin;

        maxPingouin.deplacer(maxHexa);
    }

    public void joueOrdiNiv4() {

        int maxDepl = -1;
        Hexa maxHexa = null;
        Pingouin maxPingouin = null;
        for (Pingouin p : this.pingouins) {
            for (Hexa temp : p.hexa.deplacementsPossibles()) {
                if (temp.deplacementsPossibles(p.hexa).size() > maxDepl) {
                    maxDepl = temp.deplacementsPossibles().size();
                    maxHexa = temp;
                    maxPingouin = p;
                }
            }
        }

        Partie.ressources.PingouinSelectionne = maxPingouin;

        maxPingouin.deplacer(maxHexa);
    }

    public void joueOrdiNiv5() {

        int maxCombo = -1;
        Hexa maxHexa = null;
        Pingouin maxPingouin = null;
        for (Pingouin p : this.pingouins) {
            for (Hexa temp : p.hexa.deplacementsPossibles()) {
                if (temp.deplacementsPossibles().size() * temp.nbPoisson > maxCombo) {
                    maxCombo = temp.deplacementsPossibles().size() * temp.nbPoisson;
                    maxHexa = temp;
                    maxPingouin = p;
                }
            }
        }

        Partie.ressources.PingouinSelectionne = maxPingouin;

        maxPingouin.deplacer(maxHexa);
    }

    public void joueOrdiNiv6() {

        int maxCombo = -1;
        Hexa maxHexa = null;
        Pingouin maxPingouin = null;
        for (Pingouin p : this.pingouins) {
            for (Hexa temp : p.hexa.deplacementsPossibles()) {
                System.out.println(" combo = " + temp.deplacementsPossibles(p.hexa).size() * temp.nbPoisson);
                if (temp.deplacementsPossibles(p.hexa).size() * temp.nbPoisson > maxCombo) {
                    maxCombo = temp.deplacementsPossibles().size() * temp.nbPoisson;
                    maxHexa = temp;
                    maxPingouin = p;
                    System.out.println("maxCombo = " + maxCombo);
                }
            }
        }

        System.out.println("maxCombo = " + maxCombo + " sur " + maxHexa.num);
        
        Partie.ressources.PingouinSelectionne = maxPingouin;

        maxPingouin.deplacer(maxHexa);
    }

    public void choixOrdi() {

        int alea;

        Hexa h = null;

        while (h == null || h.nbPoisson != 1) {
            alea = (int) (Math.random() * 60);
            h = Hexa.getCase(alea);
        }

        if (Partie.placeLibre(h.num)) {
            Partie.ressources.PingouinSelectionne.hexa = h;
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Partie.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            choixOrdi();
        }
    }
}
